package com.apexwear.apexutils.ui;

import android.os.Bundle;
import android.text.TextUtils;
import android.util.Log;
import android.view.View;

import androidx.annotation.Nullable;
import androidx.appcompat.app.AppCompatActivity;
import androidx.databinding.DataBindingUtil;
import androidx.lifecycle.Observer;
import androidx.lifecycle.ViewModelProvider;

import com.apex.bluetooth.core.EABleManager;
import com.apex.bluetooth.enumeration.EABleConnectState;
import com.apex.bluetooth.model.EABleMonitorReminder;
import com.apexwear.apexutils.R;
import com.apexwear.apexutils.api.OnPositiveClickListener;
import com.apexwear.apexutils.data.ReminderData;
import com.apexwear.apexutils.databinding.ActivityReminderBinding;
import com.apexwear.apexutils.dialog.MultiDialog;
import com.apexwear.apexutils.dialog.SelectDialog;
import com.apexwear.apexutils.model.MultiData;
import com.apexwear.apexutils.utils.ShowToast;

public class ReminderActivity extends AppCompatActivity {
    ActivityReminderBinding dataBinding;
    SelectDialog typeDialog, intervalDialog, startHourDialog, startMinuteDialog, endHourDialog, endMinuteDialog, stepsDialog, cupDialog;
    String[] typeArray, intervalArray, cycleArray, startHourArray, startMinuteArray, endHourArray, endMinuteArray, stepsArray, cupArray;
    MultiDialog cycleDialog;
    ReminderData reminderData;
    int cycleTime,intervalTime,drinkCup;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        dataBinding = DataBindingUtil.setContentView(this, R.layout.activity_reminder);
        dataBinding.tooBar.setNavigationIcon(R.mipmap.exit_page);
        setSupportActionBar(dataBinding.tooBar);
        getSupportActionBar().setTitle(null);
        if (reminderData == null) {
            reminderData = new ViewModelProvider(ReminderActivity.this).get(ReminderData.class);
        }
        dataBinding.tooBar.setNavigationOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                finish();
            }
        });
        dataBinding.rowType.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (typeArray == null) {
                    typeArray = getResources().getStringArray(R.array.reminder_type);
                }
                if (typeDialog == null) {
                    typeDialog = new SelectDialog(ReminderActivity.this, typeArray, null);
                    typeDialog.setOnPositiveClickListener(new OnPositiveClickListener<String>() {
                        @Override
                        public void clickButton(String mac) {
                            dataBinding.type.setText(mac);
                        }
                    });
                }
                if (!typeDialog.isShowing()) {
                    typeDialog.show();
                }

            }
        });
        dataBinding.rowInterval.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (intervalArray == null) {
                    intervalArray = new String[10];
                    for (int i = 0; i < 10; i++) {
                        intervalArray[i] = (i + 1) * 10 + "";
                    }
                }
                if (intervalDialog == null) {
                    intervalDialog = new SelectDialog(ReminderActivity.this, intervalArray, getString(R.string.unit) + "(" + getString(R.string.minute) + ")");
                    intervalDialog.setOnPositiveClickListener(new OnPositiveClickListener<String>() {
                        @Override
                        public void clickButton(String mac) {
                            intervalTime=Integer.valueOf(mac);
                            dataBinding.interval.setText(mac+getString(R.string.minute));
                        }
                    });
                }
                if (!intervalDialog.isShowing()) {
                    intervalDialog.show();
                }

            }
        });
        dataBinding.rowCycle.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (cycleArray == null) {
                    cycleArray = getResources().getStringArray(R.array.reminder_cycle);
                }
                if (cycleDialog == null) {
                    cycleDialog = new MultiDialog(ReminderActivity.this, cycleArray, null, cycleArray.length - 1);
                    cycleDialog.setOnPositiveClickListener(new OnPositiveClickListener<MultiData>() {
                        @Override
                        public void clickButton(MultiData multiData) {
                            dataBinding.cycle.setText(multiData.getTxt());
                            cycleTime = multiData.getCycle();

                        }
                    });
                }
                if (!cycleDialog.isShowing()) {
                    cycleDialog.show();
                }

            }

        });
        dataBinding.rowStartHour.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (startHourArray == null) {
                    startHourArray = getResources().getStringArray(R.array.hour_array);
                }
                if (startHourDialog == null) {
                    startHourDialog = new SelectDialog(ReminderActivity.this, startHourArray, null);
                    startHourDialog.setOnPositiveClickListener(new OnPositiveClickListener<String>() {
                        @Override
                        public void clickButton(String mac) {
                            dataBinding.startHour.setText(mac);
                        }
                    });
                }
                if (!startHourDialog.isShowing()) {
                    startHourDialog.show();
                }
            }
        });
        dataBinding.rowStartMinute.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (startMinuteArray == null) {
                    startMinuteArray = new String[60];
                    for (int i = 0; i < 60; i++) {
                        startMinuteArray[i] = i + "";
                    }
                }
                if (startMinuteDialog == null) {
                    startMinuteDialog = new SelectDialog(ReminderActivity.this, startMinuteArray, null);
                    startMinuteDialog.setOnPositiveClickListener(new OnPositiveClickListener<String>() {
                        @Override
                        public void clickButton(String mac) {
                            dataBinding.startMinute.setText(mac);
                        }
                    });
                }
                if (!startMinuteDialog.isShowing()) {
                    startMinuteDialog.show();
                }

            }
        });
        dataBinding.rowEndHour.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (endHourArray == null) {
                    endHourArray = getResources().getStringArray(R.array.hour_array);
                }
                if (endHourDialog == null) {
                    endHourDialog = new SelectDialog(ReminderActivity.this, endHourArray, null);
                    endHourDialog.setOnPositiveClickListener(new OnPositiveClickListener<String>() {
                        @Override
                        public void clickButton(String mac) {
                            dataBinding.endHour.setText(mac);

                        }
                    });
                }
                if (!endHourDialog.isShowing()) {
                    endHourDialog.show();
                }
            }
        });
        dataBinding.rowEndMinute.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (endMinuteArray == null) {
                    endMinuteArray = new String[60];
                    for (int i = 0; i < 60; i++) {
                        endMinuteArray[i] = i + "";
                    }
                }
                if (endMinuteDialog == null) {
                    endMinuteDialog = new SelectDialog(ReminderActivity.this, endMinuteArray, null);
                    endMinuteDialog.setOnPositiveClickListener(new OnPositiveClickListener<String>() {
                        @Override
                        public void clickButton(String mac) {
                            dataBinding.endMinute.setText(mac);
                        }
                    });
                }
                if (!endMinuteDialog.isShowing()) {
                    endMinuteDialog.show();
                }
            }
        });
        dataBinding.rowSteps.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (stepsArray == null) {
                    stepsArray = new String[10];
                    for (int i = 0; i < 10; i++) {
                        stepsArray[i] = (i + 1) * 10 + "";
                    }
                }
                if (stepsDialog == null) {
                    stepsDialog = new SelectDialog(ReminderActivity.this, stepsArray, null);
                    stepsDialog.setOnPositiveClickListener(new OnPositiveClickListener<String>() {
                        @Override
                        public void clickButton(String mac) {
                            dataBinding.steps.setText(mac);
                        }
                    });
                }
                if (!stepsDialog.isShowing()) {
                    stepsDialog.show();
                }
            }
        });
        dataBinding.rowCup.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (cupArray == null) {
                    cupArray = new String[10];
                    for (int i = 0; i < 10; i++) {
                        cupArray[i] = (i + 1) + "";
                    }
                }
                if (cupDialog == null) {
                    cupDialog = new SelectDialog(ReminderActivity.this, cupArray, getString(R.string.unit) + "(" + getString(R.string.cup) + ")");
                    cupDialog.setOnPositiveClickListener(new OnPositiveClickListener<String>() {
                        @Override
                        public void clickButton(String mac) {
                            drinkCup=Integer.valueOf(mac);
                            dataBinding.drink.setText(mac+getString(R.string.cup));
                        }
                    });
                }
                if (!cupDialog.isShowing()) {
                    cupDialog.show();
                }
            }
        });
        dataBinding.sure.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (EABleManager.getInstance().getDeviceConnectState() != EABleConnectState.STATE_CONNECTED) {
                    return;
                }
                String drink = dataBinding.drink.getText().toString();
                String type = dataBinding.type.getText().toString();
                String interval = dataBinding.interval.getText().toString();
                String cycle = dataBinding.cycle.getText().toString();
                String startHour = dataBinding.startHour.getText().toString();
                String startMinute = dataBinding.startMinute.getText().toString();
                String endHour = dataBinding.endHour.getText().toString();
                String endMinute = dataBinding.endMinute.getText().toString();
                String steps = dataBinding.steps.getText().toString();
                boolean isOpen = dataBinding.onOff.isChecked();
                if (TextUtils.isEmpty(type) || TextUtils.isEmpty(interval) || TextUtils.isEmpty(cycle) || TextUtils.isEmpty(startHour) || TextUtils.isEmpty(startMinute) || TextUtils.isEmpty(endHour) || TextUtils.isEmpty(endMinute)) {
                    ShowToast.show(ReminderActivity.this, getString(R.string.reminder_input_hint));
                    return;
                }
                EABleMonitorReminder eaBleMonitorReminder = new EABleMonitorReminder();
                eaBleMonitorReminder.setStep_threshold(Integer.valueOf(steps));
                eaBleMonitorReminder.setCup(drinkCup);
                eaBleMonitorReminder.setReminderSwitch(isOpen ? 1 : 0);
                if (type.equalsIgnoreCase(getString(R.string.drink))) {
                    eaBleMonitorReminder.setEaBleMonitorType(EABleMonitorReminder.EABleMonitorType.drink);
                } else if (type.equalsIgnoreCase(getString(R.string.wash_hands))) {
                    eaBleMonitorReminder.setEaBleMonitorType(EABleMonitorReminder.EABleMonitorType.washHands);
                }
                eaBleMonitorReminder.setInterval(intervalTime);
                eaBleMonitorReminder.setBegin_hour(Integer.valueOf(startHour));
                eaBleMonitorReminder.setBegin_minute(Integer.valueOf(startMinute));
                eaBleMonitorReminder.setEnd_minute(Integer.valueOf(endMinute));
                eaBleMonitorReminder.setEnd_hour(Integer.valueOf(endHour));
                eaBleMonitorReminder.setWeek_cycle_bit(cycleTime);
                Log.e("ReminderActivity","开始添加提醒事项");
                reminderData.setReminder(eaBleMonitorReminder).observe(ReminderActivity.this, new Observer<Boolean>() {
                    @Override
                    public void onChanged(Boolean aBoolean) {
                        if (aBoolean) {
                            ShowToast.show(ReminderActivity.this, getString(R.string.add_success));
                        } else {
                            ShowToast.show(ReminderActivity.this, getString(R.string.add_fail));
                        }
                    }
                });


            }
        });
    }

    @Override
    protected void onStart() {
        super.onStart();
        if (EABleManager.getInstance().getDeviceConnectState() == EABleConnectState.STATE_CONNECTED) {
            reminderData.getReminder().observe(ReminderActivity.this, new Observer<EABleMonitorReminder>() {
                @Override
                public void onChanged(EABleMonitorReminder eaBleMonitorReminder) {
                    if (eaBleMonitorReminder != null) {
                        int open = eaBleMonitorReminder.getReminderSwitch();
                        EABleMonitorReminder.EABleMonitorType type = eaBleMonitorReminder.getEaBleMonitorType();
                        int startHour = eaBleMonitorReminder.getBegin_hour();
                        int startMinute = eaBleMonitorReminder.getBegin_minute();
                        int cup = eaBleMonitorReminder.getCup();
                        int endHour = eaBleMonitorReminder.getEnd_hour();
                        int endMinute = eaBleMonitorReminder.getEnd_minute();
                        int interval = eaBleMonitorReminder.getInterval();
                        int steps = eaBleMonitorReminder.getStep_threshold();
                        cycleTime = eaBleMonitorReminder.getWeek_cycle_bit();
                        dataBinding.onOff.setChecked(open == 1 ? true : false);
                        dataBinding.startMinute.setText(startMinute + "");
                        dataBinding.startHour.setText(startHour + "");
                        dataBinding.steps.setText(steps + "");
                        dataBinding.interval.setText(interval + "");
                        dataBinding.drink.setText(cup + "");
                        dataBinding.endMinute.setText(endMinute + "");
                        dataBinding.endHour.setText(endHour + "");
                        if (type != null) {
                            if (type == EABleMonitorReminder.EABleMonitorType.drink) {
                                dataBinding.type.setText(getString(R.string.drink));
                            } else if (type == EABleMonitorReminder.EABleMonitorType.washHands) {
                                dataBinding.type.setText(getString(R.string.wash_hands));
                            }
                        }
                        if (cycleTime == 0) {
                            dataBinding.cycle.setText(getString(R.string.cycle_8));
                        } else if (cycleTime == 0x7F) {
                            dataBinding.cycle.setText(getString(R.string.every_day));
                        } else {
                            String remindTime = "";
                            String timeString = Integer.toBinaryString(cycleTime);
                            if (timeString.length() < 8) {
                                int size = 8 - timeString.length();
                                for (int i = 0; i < size; i++) {
                                    timeString = ("0" + timeString);
                                }
                            }
                            String sundayTag = timeString.substring(7, timeString.length());
                            String mondayTag = timeString.substring(6, 7);
                            String tuesdayTag = timeString.substring(5, 6);
                            String wednesdayTag = timeString.substring(4, 5);
                            String thursdayTag = timeString.substring(3, 4);
                            String fridayTag = timeString.substring(2, 3);
                            String saturday = timeString.substring(1, 2);
                            if (sundayTag.equalsIgnoreCase("1")) {
                                remindTime += (getString(R.string.cycle_7) + " ");
                            }
                            if (mondayTag.equalsIgnoreCase("1")) {
                                remindTime += (getString(R.string.cycle_1) + " ");
                            }
                            if (tuesdayTag.equalsIgnoreCase("1")) {
                                remindTime += (getString(R.string.cycle_2) + " ");
                            }
                            if (wednesdayTag.equalsIgnoreCase("1")) {
                                remindTime += (getString(R.string.cycle_3) + " ");
                            }
                            if (thursdayTag.equalsIgnoreCase("1")) {
                                remindTime += (getString(R.string.cycle_4) + " ");
                            }
                            if (fridayTag.equalsIgnoreCase("1")) {
                                remindTime += (getString(R.string.cycle_5) + " ");
                            }
                            if (saturday.equalsIgnoreCase("1")) {
                                remindTime += (getString(R.string.cycle_6) + " ");
                            } else {

                            }
                            dataBinding.cycle.setText(remindTime);
                        }
                    }
                }
            });
        }
    }

    @Override
    protected void onDestroy() {
        if (typeDialog != null) {
            typeDialog.destroyDialog();
            typeDialog = null;
        }
        if (intervalDialog != null) {
            intervalDialog.destroyDialog();
            intervalDialog = null;
        }
        if (startHourDialog != null) {
            startHourDialog.destroyDialog();
            startHourDialog = null;
        }
        if (startMinuteDialog != null) {
            startMinuteDialog.destroyDialog();
            startMinuteDialog = null;
        }
        if (endHourDialog != null) {
            endHourDialog.destroyDialog();
            endHourDialog = null;
        }
        if (endMinuteDialog != null) {
            endMinuteDialog.destroyDialog();
            endMinuteDialog = null;
        }
        if (stepsDialog != null) {
            stepsDialog.destroyDialog();
            stepsDialog = null;
        }
        if (cupDialog != null) {
            cupDialog.destroyDialog();
            cupDialog = null;
        }
        if (cycleDialog != null) {
            cycleDialog.destroyDialog();
            cycleDialog = null;
        }
        typeArray = null;
        intervalArray = null;
        cycleArray = null;
        startHourArray = null;
        startMinuteArray = null;
        endHourArray = null;
        endMinuteArray = null;
        stepsArray = null;
        cupArray = null;
        reminderData = null;
        if (dataBinding != null) {
            dataBinding.unbind();
            dataBinding = null;
        }
        super.onDestroy();
    }
}
